package com.hxd.otc.module.login.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.RecentLoginAccountInfoHelper;
import com.hxd.otc.module.login.bean.AccountBean;
import com.hxd.otc.module.login.contract.LoginContract;
import com.hxd.otc.module.my.bean.AppInfoBean;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.EncryptUtils;
import com.hxd.otc.utils.hyphenate.HyphenateKeFuUtil;
import com.hxd.otc.utils.rongcloud.RongCloudUtils;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/4 10:22
 * 描  述 :  ${登录Presenter}
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {
    private final UserModel mUserModel;

    public LoginPresenter(LoginContract.View view) {
        super(view);
        mUserModel = UserModel.getInstance();
    }

    @Override
    public void login(@NonNull final String account, @NonNull final String password, String sessionID, final boolean encrypted) {

        mUserModel.login(account, encrypted ? password : EncryptUtils.getRSA(password), sessionID, new BaseObserver<AccountBean>() {
            @Override
            public void onNext(String title, AccountBean resultBean) {
                mView.loginSucceed(resultBean);
                RecentLoginAccountInfoHelper.updateAccountAndPassword(account, encrypted ? password : EncryptUtils.getRSA(password));
                //登录成功后，重新获取融云token和连接环信客服
                RongCloudUtils.getInstance().requestRongCloudToken();
                HyphenateKeFuUtil.getInstance().register();
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.onLoginFail(errType, errMessage);
            }

        }, mView.<BaseBean<AccountBean>>bindAutoDispose());
    }

    @Override
    public void getUpdateVersionInfo() {
        mUserModel.getUpdateAppInfo(new BaseObserver<AppInfoBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull AppInfoBean appInfoBean) {
                mView.getUpdateVersionInfoSuccess(appInfoBean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
            }
        }, mView.<BaseBean<AppInfoBean>>bindAutoDispose());
    }


}
